package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase {

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public SortedListOf<ContactData> getUiContacts() {
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
		
		manager.navigateTo().homePage();
		List<WebElement> rowsid = driver.findElements(By.name("entry"));
		
		for (int indx = 1; indx <= rowsid.size(); indx++) {
			WebElement v_firstname = driver.findElement(By.xpath("//tr[@name='entry']["+indx+"]/td[2]"));
			String l_firstname = v_firstname.getText();
			
			WebElement v_lastname = driver.findElement(By.xpath("//tr[@name='entry']["+indx+"]/td[3]"));
			String l_lastname = v_lastname.getText();
			
			WebElement v_firstemail = driver.findElement(By.xpath("//tr[@name='entry']["+indx+"]/td[4]"));
			String l_firstemail = v_firstemail.getText();
			
			WebElement v_homephone = driver.findElement(By.xpath("//tr[@name='entry']["+indx+"]/td[5]"));
			String l_homephone = v_homephone.getText();
			
			contacts.add(new ContactData()
					.withFirstname(l_firstname)
					.withLastname(l_lastname)
					.withFirstemail(l_firstemail)
					.withHomephone(l_homephone));
 		}
		return contacts;
	}

	public ContactHelper createContact(ContactData contact, boolean formtype) {
		manager.navigateTo().homePage();
		initContactCreation();
		fillContactForm(contact, formtype);
		submitContactCreation();
		manager.navigateTo().returnToHomePage();
		manager.getModel().addContact(contact);
		return this;
	}

	public ContactHelper modifyContact(ContactData contact, int indx, boolean formtype) {
		manager.navigateTo().homePage();
		selectContact(indx);
		fillContactForm(contact, formtype);
		submitContactModification();
		manager.navigateTo().returnToHomePage();
		manager.getModel().deleteContact(indx).addContact(contact);
		return this;
	}

	public ContactHelper deleteContactByIndex(int i_index) {
		manager.navigateTo().homePage();
		selectContact(i_index);
		submitContactDeletion();
		manager.navigateTo().returnToHomePage();
		manager.getModel().deleteContact(i_index);
		return this;
	}

	//-----------------------------------------------------------------------------------
	
	public ContactHelper selectContact(int i_index) {
		click(By.xpath("//tr[@name='entry']["+(i_index+1)+"]/td[7]/a/img"));
		return this;
	}

	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formtype) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHomephone());
		type(By.name("mobile"), contact.getMobilephone());
		type(By.name("work"), contact.getWorkphone());
		type(By.name("email"), contact.getFirstemail());
		type(By.name("email2"), contact.getSecondemail());
		selectByText(By.name("bday"), contact.getBirthday());
		selectByText(By.name("bmonth"), contact.getBirthmonth());
		type(By.name("byear"), contact.getBirthyear());
		
		if (formtype == CREATION){
			//selectByText(By.name("new_group"), contact.i_group);
		}
		else {
			if( driver.findElements(By.name("new_group")).size() != 0 ){
				throw new Error("Group selector exists in contact modification form");
			}
		}
		//selectByText(By.name("new_group"), contact.group);
		type(By.name("address2"), contact.getSecondaddress());
		type(By.name("phone2"), contact.getSecondphone());
		
		return this;
	}


	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}

	public ContactHelper createOneMoreContact() {
		click(By.linkText("add next"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.name("update"));
		return this;
	}

	private void submitContactDeletion() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}
}
